/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Ventana.java
 *
 * Created on 20/06/2011, 10:35:05 AM
 */
package zonasoft.Vistas;

//import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;
import com.mysql.jdbc.Connection;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.ListModel;
import javax.swing.table.DefaultTableModel;
import zonasoft.Conceptos.Puesto;
import zonasoft.Manejadores.ManejadorBaseDatos;
import zonasoft.Manejadores.ManejadorPuesto;
import zonasoft.Manejadores.ManejadorQuerys;

/**
 *
 * @author campo
 */
public class ConsultaAvanzada extends javax.swing.JDialog {

    private List<Puesto> puestosDeVotacion = new ArrayList<Puesto>();
    private HashMap<String, Integer> puestos;

    /** Creates new form Ventana */
    public ConsultaAvanzada(java.awt.Frame parent) {
        super(parent, true);
        initComponents();
        pack();
        Rectangle parentBounds = parent.getBounds();
        Dimension size = getSize();

        int x = Math.max(0, parentBounds.x + (parentBounds.width - size.width) / 2);
        int y = Math.max(0, parentBounds.y + (parentBounds.height - size.height) / 2);
        setLocation(new Point(x, y));
    }

    public void limpiarxlistaPuesto() {

        DefaultListModel dlm = new DefaultListModel();
        dlm.setSize(0);
        xlistaPuestos.setModel(dlm);
        String[] datos = {""};
//  public int getSize(){ return datos.length;}
//  public Object getElementAt(int i ) {}

        // DefaultListModel dtm =  (DefaultListModel) xlistaPuestos.getModel();


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXTaskPaneContainer1 = new org.jdesktop.swingx.JXTaskPaneContainer();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        xlistaPuestos = new org.jdesktop.swingx.JXList();
        jXLabel1 = new org.jdesktop.swingx.JXLabel();
        botonListar = new javax.swing.JButton();
        departamento = new javax.swing.JTextField();
        municipio = new javax.swing.JTextField();
        barrio = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listaLideres = new javax.swing.JList();
        jLabel5 = new javax.swing.JLabel();
        puesto = new javax.swing.JLabel();
        jXTitledPanel1 = new org.jdesktop.swingx.JXTitledPanel();
        totalZonificados = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Lucida Bright", 1, 18));
        jLabel1.setText("Consulta Avanzada");
        jXTaskPaneContainer1.add(jLabel1);

        xlistaPuestos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                xlistaPuestosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(xlistaPuestos);

        jXLabel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED, java.awt.Color.lightGray, java.awt.Color.lightGray, java.awt.Color.darkGray, java.awt.Color.darkGray));
        jXLabel1.setText("Puestos de Votacion");

        botonListar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        botonListar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zonasoft/Vistas/iconos/Column-Chart-icon.png"))); // NOI18N
        botonListar.setText("Listar");
        botonListar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonListarActionPerformed(evt);
            }
        });

        jLabel2.setText("Departamento");

        jLabel3.setText("Municipio");

        jLabel4.setText("Barrio");

        listaLideres.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listaLideresMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(listaLideres);

        jLabel5.setFont(new java.awt.Font("Tahoma", 2, 18)); // NOI18N
        jLabel5.setText("Lideres del Puesto: ");

        puesto.setFont(new java.awt.Font("Tahoma", 2, 14)); // NOI18N
        puesto.setText(".");

        jXTitledPanel1.setTitle("          Total Zonificados");
        jXTitledPanel1.setTitleFont(new java.awt.Font("Tahoma", 3, 14)); // NOI18N

        totalZonificados.setFont(new java.awt.Font("Tahoma", 2, 90)); // NOI18N

        javax.swing.GroupLayout jXTitledPanel1Layout = new javax.swing.GroupLayout(jXTitledPanel1.getContentContainer());
        jXTitledPanel1.getContentContainer().setLayout(jXTitledPanel1Layout);
        jXTitledPanel1Layout.setHorizontalGroup(
            jXTitledPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXTitledPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(totalZonificados, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jXTitledPanel1Layout.setVerticalGroup(
            jXTitledPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXTitledPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(totalZonificados, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jXTaskPaneContainer1, javax.swing.GroupLayout.PREFERRED_SIZE, 941, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(5, 5, 5)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(5, 5, 5)
                                .addComponent(jLabel2))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(5, 5, 5)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(botonListar, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                                    .addComponent(jLabel3)
                                    .addComponent(municipio, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                                    .addComponent(departamento)
                                    .addComponent(jLabel4)
                                    .addComponent(barrio, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                                    .addComponent(jXTitledPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                    .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel5)
                        .addComponent(puesto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(48, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXTaskPaneContainer1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 444, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2)
                                    .addComponent(puesto))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(departamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(municipio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(barrio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(botonListar, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(41, 41, 41)
                                        .addComponent(jXTitledPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE))))
                        .addGap(11, 11, 11))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonListarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonListarActionPerformed
        // TODO add your handling code here:
        limpiarxlistaPuesto();
        listaraPuestos();
    }//GEN-LAST:event_botonListarActionPerformed

    private void xlistaPuestosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_xlistaPuestosMouseClicked
        ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
        List<String> result = new ArrayList<String>();
        DefaultListModel modelo = new DefaultListModel();

        try {

            mbd.conectar();
            ManejadorQuerys.con = (Connection) mbd.getConexion();

            if (evt.getClickCount() == 1) {

                int index = xlistaPuestos.locationToIndex(evt.getPoint());
                ListModel dlm = xlistaPuestos.getModel();
                Puesto p = (Puesto) dlm.getElementAt(index);
                xlistaPuestos.ensureIndexIsVisible(index);

                result = ManejadorQuerys.AllLideresByPuesto(p.getIdPuesto());
                if (result.size() > 0) {
                    String[] datos = result.get(0).split("~");
                    departamento.setText(datos[4]);
                    municipio.setText(datos[3]);
                    barrio.setText(datos[2]);
                    puesto.setText(datos[1]);

                    for (String r : result) {
                        String[] res = r.split("~");
                        modelo.addElement(res[0].toUpperCase());
                    }
                } else {
                    modelo.addElement("No hay Lideres Asociados".toUpperCase());
                }

                listaLideres.setModel(modelo);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }//GEN-LAST:event_xlistaPuestosMouseClicked

    private void listaLideresMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaLideresMouseClicked
        // TODO add your handling code here:

        if (evt.getClickCount() == 1) {
            int index = listaLideres.locationToIndex(evt.getPoint());
            String lider = listaLideres.getSelectedValue().toString();
//            System.out.println("lider = " + lider);
//            if (!lider.isEmpty()) {
                try {
                    //   String lider = listaLideres.;
                    int total = ManejadorQuerys.totalZonificadosbyLider(lider);
//                    System.out.println("total = " + total);
                    totalZonificados.setText(String.valueOf(total));
                
                } catch (Exception ex) {
                    Logger.getLogger(ConsultaAvanzada.class.getName()).log(Level.SEVERE, null, ex);
                }

//            }else {
//            }
        }
//         int numAleatorio = (int)((Math.random() * 100) + 1);
//         totalZonificados.setText(String.valueOf(numAleatorio));
    }//GEN-LAST:event_listaLideresMouseClicked

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//
//        new ConsultaAvanzada(null).setVisible(true);
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
////                ConsultaAvanzada dialog = new ConsultaAvanzada(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//               // dialog.setVisible(true);
//            }
//        });
//    }
    public void listaraPuestos() {
        ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
        puestos = new HashMap<String, Integer>();
        DefaultListModel modelo = new DefaultListModel();


        try {
            mbd.conectar();
            ManejadorPuesto.con = (Connection) mbd.getConexion();
            List listarPuestos = null;

            listarPuestos = ManejadorPuesto.listarPuestos();
            Iterator<Puesto> iter = listarPuestos.iterator();
            Puesto pue = null;


            while (iter.hasNext()) {
                pue = iter.next();
//                    System.out.println("pue.getIdPuesto() = " + pue.getIdPuesto());
                int codigoPuesto = Integer.parseInt(pue.getIdPuesto());
                String nombre = pue.getNombrePuesto();
                puestos.put(nombre, codigoPuesto);
                modelo.addElement(pue);
//           puestos2.put(pue.getIdPuesto(), nombre );


            } //agregamos el modelo a la lista
            xlistaPuestos.setModel(modelo);


        } catch (Exception ex) {
            ex.printStackTrace();


        }
    }// fin del listar Puestos
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField barrio;
    private javax.swing.JButton botonListar;
    private javax.swing.JTextField departamento;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private org.jdesktop.swingx.JXLabel jXLabel1;
    private org.jdesktop.swingx.JXTaskPaneContainer jXTaskPaneContainer1;
    private org.jdesktop.swingx.JXTitledPanel jXTitledPanel1;
    private javax.swing.JList listaLideres;
    private javax.swing.JTextField municipio;
    private javax.swing.JLabel puesto;
    private javax.swing.JLabel totalZonificados;
    private org.jdesktop.swingx.JXList xlistaPuestos;
    // End of variables declaration//GEN-END:variables
}
